<?php
	//GLOBALS MUST BE HERE BEFORE THE COMMON.PHP FILE.
	$projects_folder = "./projects/";

	include_once "boiler_plate/common.php"; //MUST ALWAYS BE THE LAST INCLUDE
	//----------------------------------------------------------------------

	function project_save($obj){
		global $projects_folder;

		//FIRST CREATE THE PROJECT FOLDER IF IT DOES NOT EXIST
		$folder = $projects_folder.$obj->data->name;
		if (!file_exists($folder)){
			mkdir($folder);
		}
		//NOW SAVE THE JSON TO A FILE
		$file = $folder."/project.json";
		file_put_contents($file, json_encode($obj->data));

		exit_with_response(0);
	}
	
	function project_load($obj){
		global $projects_folder;

		//FIRST CREATE THE PROJECT FOLDER IF IT DOES NOT EXIST
		$folder = $projects_folder.$obj->data->project;
		if (!file_exists($folder)){
			exit_with_response(-1, "Project cannot be located");
		}
		
		//NOW SAVE THE JSON TO A FILE
		$file = $folder."/project.json";
		$contents = trim(file_get_contents($file));
		
		exit_with_response(0, "", $contents);
	}

	function project_file_save($obj){
		$base_path = "./temp/";

		$filename = $base_path."project.erd";

		if (file_exists($base_path) == false)
			exit_with_response(-1, "Could not locate base-path: ".$base_path);

		if (file_put_contents($filename, json_encode($obj->data,2)) === false){
			exit_with_response(-1, "Failed to create file");
		}

		$result["filename"] = "./boiler_plate/".$filename;
		exit_with_response(0, "", json_encode($result));
	}
	
	function project_list($obj){
		global $projects_folder;
		$result = array();
		$result["projects"] = array();
		$blacklist = array(".", "..");
		$handle = opendir($projects_folder);
		if ($handle){
			while (false !== ($entry = readdir($handle))) {
				if (in_array($entry, $blacklist)) continue;
				if (is_dir($projects_folder.$entry))
					array_push($result["projects"], $entry);
			}
		}
		
		exit_with_response(0, "", json_encode($result));
	}
	
	function project_generate_website($obj){
		global $projects_folder;
		$result = array();
		
		//FIRST CREATE THE PROJECT FOLDER IF IT DOES NOT EXIST
		$folder = $projects_folder.$obj->data->name;
		if (!file_exists($folder)){
			mkdir($folder);
		}
		
		$website_folder = $projects_folder.$obj->data->name."/website/";
		if (file_exists($website_folder)){
			directory_remove($website_folder);
		}
		
		mkdir($website_folder);
		directory_copy("./boiler_plate/", $website_folder);
		mkdir($website_folder."/db_script/");
		file_put_contents($website_folder."/db_script/db.sql", $obj->sql_code);
		
		//NOW SAVE THE JSON TO A FILE
		$file = $folder."/project.json";
		file_put_contents($file, json_encode($obj->data));
		
		//NOW CREATE JSON FORMS
		$json = "";
		foreach($obj->data->erd as $erd){
			foreach($erd->entities as $obj){
				$json .= "auto_maintain_".$obj->sql_table." = ". json_encode($obj).";\r\n\r\n";
			}
		}
		file_put_contents($website_folder."/js/lookup_editors.js", $json);
		$scriptname=end(explode('/',$_SERVER['PHP_SELF']));
		$scriptpath=str_replace($scriptname,'',$_SERVER['PHP_SELF']);
		$result["url"] = "http://".$_SERVER["SERVER_NAME"]."/".$scriptpath.str_replace(" ", "%20",$website_folder);
		
		exit_with_response(0, "", json_encode($result));
	}
	
	//Delete folder function
	function directory_remove($dir) {
		if (!file_exists($dir)) return true;
		if (!is_dir($dir) || is_link($dir)) return unlink($dir);

		foreach (scandir($dir) as $item) {
			if ($item == '.' || $item == '..') continue;
			if (!directory_remove($dir . "/" . $item)) {
				chmod($dir . "/" . $item, 0777);
				if (!directory_remove($dir . "/" . $item)) return false;
			};
		}
		return rmdir($dir);
	}
	
	function directory_copy( $source, $target ) {
		if ( is_dir( $source ) ) {
			@mkdir( $target );
			$d = dir( $source );
			while ( FALSE !== ( $entry = $d->read() ) ) {
				if ( $entry == '.' || $entry == '..' ) {
					continue;
				}
				$Entry = $source . '/' . $entry; 
				if ( is_dir( $Entry ) ) {
					directory_copy( $Entry, $target . '/' . $entry );
					continue;
				}
				copy( $Entry, $target . '/' . $entry );
			}

			$d->close();
		}else {
			copy( $source, $target );
		}
	}


?>
